<template>
  <!-- 分层设置列表 -->
  <div class="app-container">
    <div class="form-container">
      <el-form :inline="true" label-width="80px" :model="filters" class="selectFrom">
        <el-form-item label="层号" prop="layerNum">
          <label slot="label" v-html="`层&emsp;&emsp;号`" />
          <el-input v-model="filters.layerNum" size="small" placeholder="请输入层号" />
        </el-form-item>
        <el-form-item label="名称" prop="setName">
          <label slot="label" v-html="`名&emsp;&emsp;称`" />
          <el-input v-model="filters.setName" size="small" placeholder="请输入名称" />
        </el-form-item>
      </el-form>
    </div>
    <!--操作按钮-->
    <div class="btns-group">
      <el-button v-clickForbidden icon="el-icon-search" size="middle" @click="handleQuery">搜索</el-button>
      <el-button v-clickForbidden icon="el-icon-refresh" size="middle" @click="resetQuery">重置</el-button>
      <el-button icon="el-icon-plus" size="middle" @click="handleAdd">新增</el-button>
      <el-button icon="el-icon-edit" size="middle" :disabled="isDetail" @click="handleUpdate">修改</el-button>
      <el-button icon="el-icon-zoom-in" size="middle" :disabled="isDetail" @click="handleDetail">详情</el-button>
      <span class="personal-type">
        <el-button icon="el-icon-arrow-left" size="middle" @click="handleReturn()">返回</el-button>
      </span>
    </div>
    <!-- 表格展示start -->
    <el-table
      ref="multipleTable"
      v-loading="tableLoading"
      stripe
      class="table-list"
      :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
        (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
      :data="tableData"
      :row-style="rowClass"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="38" align="center" />
      <!--      <el-table-column type="index" label="序号" width="51" align="center" />-->
      <el-table-column prop="layerNum" label="层号" min-width="180" align="center" />
      <el-table-column prop="setName" label="名称" min-width="100" align="center" />
      <el-table-column
        prop="describe"
        :show-overflow-tooltip="true"
        label="描述"
        min-width="200"
        align="center"
      />
    </el-table>
    <!-- 表格展示end-->
    <!-- 分页-->
    <pagination
      :total="total"
      :page.sync="filters.pageNum"
      :limit.sync="filters.pageSize"
      @pagination="getList"
    />
    <!-- 人员新增弹框 -->
    <addDialog ref="addDialog" />
    <!-- 人员详情弹框 -->
    <detailDialog ref="detailDialog" />

  </div>
</template>

<script>
// api
import { listHierarchicaSetting } from '@/api/projectManage/projectData/LayeredSettings'
// 新增弹框
import addDialog from './components/addDialog'
// 详情弹框
import detailDialog from './components/detailDialog'
import { mapState } from 'vuex'
export default {
  name: 'PersonnelType',
  components: {
    detailDialog,
    addDialog
  },
  data() {
    return {
      tableLoading: false, // 表格加载
      highLevelSet: false, // 高级搜索
      // 筛选条件
      filters: {
        proId: this.$store.state.basicInfo.proId,
        layerNum: '', // 层号
        setName: '', // 名称
        pageNum: 1,
        pageSize: this.fristPageSize
      },
      // 分页总条数
      total: 0,
      // 非单个禁用
      single: true,
      isDetail: true,
      // 非多个禁用
      multiple: true,
      // 选中表格对话框的存储的值
      multipleSelection: [],
      // 高亮样式
      selectRow: [],
      // 表格数据
      tableData: []
    }
  },
  computed: {
    ...mapState({
      needTagsView: (state) => state.settings.tagsView
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    }
  },
  mounted() {
    // 获取列表数据
    this.getList()
  },
  methods: {
    // 获取数据
    async getList() {
      this.tableLoading = true
      try {
        const { data: { rows, total }} = await listHierarchicaSetting(this.filters)
        this.tableData = rows
        this.total = total
        this.tableLoading = false
        // this.addTaleData()
      } catch {
        this.tableLoading = false
      }
    },
    // 补空白行
    addTaleData() {
      if (this.tableData.length < this.filters.pageSize) {
        var arr = {}
        this.tableData.push(arr)
        this.addTaleData()
      }
    },
    /* 表格选中行事件 start */
    // 单击每行时每行的复选框选中
    rowChecked(row) {
      // 将选中的数组赋给常量 selectData
      const selectData = this.multipleSelection
      // 调用表格的方法将所有已选的都取消
      this.$refs.multipleTable.clearSelection()
      // 判断选中的数组个数是不是等于 1
      if (selectData.length === 1) {
        // 循环判断有没有和当前行的相同的
        selectData.forEach((item) => {
          // 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        // 不等于1时
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(val) {
      if (JSON.stringify(val[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
        val = []
      }

      this.selectRow = []
      this.multipleSelection = val
      this.isDetail = val.length !== 1
      this.single = val.length === 0 || val[0].isFixed === '0'
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item, index) => {
          this.selectRow.push(this.tableData.indexOf(item))
        })
      }
      if (val.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(val.pop())
      }
    },
    /* 表格选中行事件 end */

    /* 按钮 start */
    // 查询
    handleQuery() {
      this.getList()
    },
    // 重置搜索条件
    resetQuery() {
      this.filters.layerNum = ''
      this.filters.setName = ''
      this.getList()
    },
    // 新增
    handleAdd() {
      this.$refs.addDialog.handleAdd('新增分层')
    },
    // 编辑
    handleUpdate() {
      this.$refs.addDialog.handleAdd('修改分层', this.multipleSelection[0].id)
    },
    // 详情
    handleDetail() {
      this.$refs.detailDialog.handleAdd('分层详情', this.multipleSelection[0].id)
    },
    // 返回
    handleReturn() {
      this.$router.go(-1)
    }
    /* 按钮 end */
  }
}
</script>

